class Product(object):
    def __init__(self, product_id, section_num, route_id):
        self.product_id = product_id.strip()
        self.section_num = int(section_num.strip())
        self.route_id = route_id.strip()


class Route(object):

    def __init__(self, route_id, route_nums, route_names,
                 equip_types, cost_time_low, cost_time_high, units, ready_times):
        self.route_id = route_id
        self.route_nums = list(map(str, [x.strip() for x in route_nums]))
        self.route_names = [x.strip() for x in route_names]
        self.equip_types = [x.strip() for x in equip_types]
        self.cost_time_low = [Route.get_mim(x) for x in cost_time_low]
        self.cost_time_high = [Route.get_mim(x) for x in cost_time_high]
        self.units = [x.strip() for x in units]
        self.ready_times = [Route.get_mim(x) if len(x) > 0 else 0 for x in ready_times]

    @classmethod
    def get_mim(cls, x):
        if len(x) > 0 and x[-1] == 'h':
            return float(x[:-1].strip()) * 60 // 1
        if len(x) > 3 and x[-3:] == 'min':
            return int(x[:-3])
        return int(x)


class Equipment(object):
    def __init__(self, equip_name, equip_type, unit):
        self.equip_name = equip_name.strip()
        self.equip_type = equip_type.strip()
        self.unit = unit.strip()


class Data:
    def __init__(self, product_path="data/产品信息.csv",
                 equip_path="data/设备信息.csv",
                 route_path="data/工艺路线.csv"):
        self.products = []
        self.equips = []
        self.routes = []
        with open(product_path, 'r', encoding='utf-8') as f:
            for line in f.readlines()[1:]:
                self.products.append(Product(*line.strip().split(',')))
        self.products.sort(key=lambda x: (x.route_id, x.section_num))

        with open(equip_path, 'r', encoding='gbk') as f:
            for line in f.readlines()[1:]:
                self.equips.append(Equipment(*line.strip().split(',')))

        route_d = {}
        with open(route_path, 'r', encoding='gbk') as f:
            for line in f.readlines()[1:]:
                line = line.strip().split(',')
                if line[0] not in route_d:
                    route_d[line[0]] = []
                route_d[line[0]].append(line[1:])

        for k, v in route_d.items():
            self.routes.append(
                Route(
                    k,
                    [x[0] for x in v],
                    [x[1] for x in v],
                    [x[2] for x in v],
                    [x[3].strip().split('-')[0] for x in v],
                    [x[3].strip().split('-')[-1] for x in v],
                    [x[4] for x in v],
                    [x[5] for x in v],
                )
            )
